package com.library.recommendation.service;

import com.library.book.service.BookServiceImpl;
import com.library.entity.*;
import com.library.recommendBook.service.RecommendBookServiceImpl;
import com.library.recommendBook.util.RecommendReason;
import com.library.recommendation.dao.RecommendationDaoImpl;
import com.library.relation.service.BookRelationServiceImpl;
import com.library.staff.service.StaffServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Service
@Transactional(readOnly=false)
public class RecommendationServiceImpl {
	
	@Resource
	private RecommendationDaoImpl recommendationDaoImpl;
	@Resource
	private RecommendBookServiceImpl recommendBookServiceImpl;
	@Resource
	private BookServiceImpl bookServiceImpl;
	@Resource
	private BookRelationServiceImpl bookRelationServiceImpl;
	@Resource
	private StaffServiceImpl staffServiceImpl;

	public void addRecommendation(Recommendation recommendation){
		this.recommendationDaoImpl.saveRecommendation(recommendation);
	}

    @Transactional(readOnly=true)
	public Recommendation getRecommendationById(Integer recommendationId)
	{
		return this.recommendationDaoImpl.getRecommendationById(recommendationId);
	}

    @Transactional(readOnly = true)
	public Recommendation getRecommendationByStaff(Staff staff){
		return this.getRecommendationById(staff.getRecommendation().getRecommendationId());
	}

	public void updateRecommendation(Recommendation recommendation){
		this.recommendationDaoImpl.updateRecommendation(recommendation);
	}

	/*删除指定推荐*/
	public void deleteRecommendation(int recommendationId){
		Recommendation recommendation = this.recommendationDaoImpl.getRecommendationById(recommendationId);
		this.recommendationDaoImpl.deleteRecommendation(recommendation);
    }

    /*通过指定员工和推荐的读友来 更新推荐图书内容 */
    public void updateRecommendationBooksByStaffs(Recommendation recommendation,List<Staff> staffs){
		Set<Book> books = recommendation.getStaff().getBooks();		//员工有过行为的书籍
		Set<RecommendBook> recommendBooks = recommendation.getRecommendBooks();		//推荐图书集合
		List<Book> hotBooks = this.bookServiceImpl.getAllClassHotBook(10);		//热门书籍
		Staff s;
		Book book;
		RecommendBook recommendBook;

		Iterator<Staff> staffIterator = staffs.iterator();
		Iterator<Book> bookIterator;

		/*先清空历史推荐*/
		recommendBooks.clear();

		/*基于相似读友来添加推荐的图书*/
		while(staffIterator.hasNext()){
			s = staffIterator.next();

			bookIterator = s.getBooks().iterator();
			while(bookIterator.hasNext()){
				book = bookIterator.next();
				if(books.contains(book)){	//读者有过行为	则不添加到借阅列表中
					continue;
				} else if(hotBooks.contains(book)){		//热门书籍不推荐
					continue;
				}
				recommendBook = new RecommendBook();
				recommendBook.setBook(book);
				recommendBook.setReason(s.getName() + RecommendReason.BORROWED_FRIEND.getName());

				if(recommendBooks.size() > 2){
					break;
				} else if(!(this.checkRecommendBookExist(recommendBooks,recommendBook))){
					this.recommendBookServiceImpl.addRecommendBook(recommendBook);		//添加推荐条目
					recommendBooks.add(recommendBook);		//添加到推荐列表
				}
			}	//while
		}//while

		//获得相似的图书
		Set<BookRelation> similarBookRelations = bookRelationServiceImpl.getRecommendationBookRelationByStaff(recommendation.getStaff());
		Iterator<BookRelation>bookRelationIterator = similarBookRelations.iterator();
		BookRelation bookRelation = null;

		while(bookRelationIterator.hasNext()){
			bookRelation = bookRelationIterator.next();
			if(books.contains(bookRelation.getBookTwo())){	//读者有过行为	则不添加到借阅列表中
				continue;
			} else if(hotBooks.contains(bookRelation.getBookTwo())){		//热门书籍不推荐
				continue;
			}
			recommendBook = new RecommendBook();
			recommendBook.setBook(bookRelation.getBookTwo());
			recommendBook.setReason("《" + bookRelation.getBookOne().getName() + "》" + RecommendReason.BORROWED_RELATED.getName());

			if(recommendBooks.size() > 10){
				break;
			}else if(!checkRecommendBookExist(recommendBooks,recommendBook)){
				this.recommendBookServiceImpl.addRecommendBook(recommendBook);		//添加推荐条目
				recommendBooks.add(recommendBook);		//添加到推荐列表
			}
		}	//end while

		recommendation.setRecommendBooks(recommendBooks);
		this.updateRecommendation(recommendation);	//更新推荐
	}

	/* 判断推荐图书是否已经在推荐列表中 */
	private boolean checkRecommendBookExist(Set<RecommendBook> recommendBooks,RecommendBook recommendBook){
		Iterator<RecommendBook> recommendBookIterator = recommendBooks.iterator();
		boolean flag = false;
		RecommendBook b;

		while(recommendBookIterator.hasNext()){
			b = recommendBookIterator.next();
			if(b.getBook().getBookId() == recommendBook.getBook().getBookId()){
				flag = true;
				break;
			}
		}	//while

		return flag;
	}
}
